package com.joinway.framework.admin.core.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;

import com.baidu.ueditor.ActionEnter;
import com.joinway.framework.admin.core.service.file.FileManager;
import com.joinway.framework.bean.type.Server;
import com.joinway.framework.core.repository.SystemConfigRepository;
import com.joinway.framework.core.utils.FrameworkHelper;

@Deprecated
public class UEditorServlet extends HttpServlet {

	private final static Logger log = LoggerFactory.getLogger(UEditorServlet.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	final static List<String> uploadPrefixes = Arrays.asList(
		new String[]{
				"imageUrlPrefix"
				, "scrawlUrlPrefix"
				, "snapscreenUrlPrefix"
				, "catcherUrlPrefix"
				, "videoUrlPrefix"
				, "fileUrlPrefix"
				, "imageManagerUrlPrefix"
				, "fileManagerUrlPrefix"
		}
	);
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			SystemConfigRepository repository = FrameworkHelper.getBean(SystemConfigRepository.class);
			
			String path = repository.getStringValue(SystemConfigRepository.UEditorPath);
			String requestPath = request.getServletPath();
			
			if(StringUtils.trimToEmpty(path).equals(requestPath)){
				String rootPath = request.getServletContext().getRealPath("/");
				
				String physicalPath = FileManager.getLocalPath("upload", Server.Web, request);
				
				log.info("File physical path {}", physicalPath);
				
				FileUtils.forceMkdir(new File(physicalPath));
				
				String output = new ActionEnter( request, rootPath, physicalPath).exec();
				
				if("config".equals(request.getParameter("action"))){
					// send back config.json, change the upload prefix
					String context = request.getContextPath();
					
					JSONObject json = new JSONObject(output);
					for(String prefix : uploadPrefixes){
						String uploadPath = context + json.get(prefix).toString();
						json.put(prefix, uploadPath);
					}
					
					output = json.toString();
				}
				
				response.setHeader("Content-Type" , MediaType.TEXT_PLAIN_VALUE);
				
				IOUtils.write(output, response.getOutputStream());
			}else{
				response.sendError(HttpServletResponse.SC_BAD_REQUEST, "invalid path " + requestPath);
			}
			
		}catch(Exception e){
			throw new ServletException(e);
		}
	}

}
